﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace ONCALL_ENTRY{
    /// <summary>
    /// Summary description for OnCallEntry
    /// </summary>
    public class OnCallEntry
    {
        #region Globals

        private int scheduleID;
        private int clientID;
        private string note;
        private SqlDateTime startTime;
        private SqlDateTime endTime;
        private SqlDateTime startDate;
        private SqlDateTime endDate;
        private string daysOfMonth;

        public bool daily, weekly, monthly;
        public bool mon, tue, wed, thu, fri, sat, sun;

        #endregion

        #region Constructors

        public OnCallEntry()
        {
            setNull();
        }
        public OnCallEntry(int sID, int cID)
        {
            setNull();
            scheduleID = sID;
            clientID = cID;
        }
        public OnCallEntry(int sID, int cID, string msg)
        {
            setNull();
            scheduleID = sID;
            clientID = cID;
            note = msg;
        }
        public OnCallEntry(int sID, int cID, string msg, SqlDateTime sT, SqlDateTime eT, SqlDateTime sD)
        {
            setNull();
            scheduleID = sID;
            clientID = cID;
            note = msg;
            startTime = sT;
            endTime = eT;
            startDate = sD;
        }
        public OnCallEntry(int sID, int cID, string msg, SqlDateTime sT, SqlDateTime eT, SqlDateTime sD, SqlDateTime eD)
        {
            setNull();
            scheduleID = sID;
            clientID = cID;
            note = msg;
            startTime = sT;
            endTime = eT;
            startDate = sD;
            endDate = eD;
        }
        public OnCallEntry(SqlDataReader reader)
        {
            setNull();         
                scheduleID = reader.GetInt32(0);
                clientID = reader.GetInt32(1);
                startDate = reader.GetSqlDateTime(2);
                startTime = reader.GetSqlDateTime(3);
                endTime = reader.GetSqlDateTime(4);
                try { endDate = reader.GetSqlDateTime(5); } catch (Exception e) { endDate = startDate; }
                try { note = reader.GetString(6); } catch (SqlNullValueException e) { note = ""; }
                setRecurrence(reader.GetBoolean(7), reader.GetBoolean(8), reader.GetBoolean(16));
                setWeeklyRecurrence(String.Format("{0}{1}{2}{3}{4}{5}{6}",
                    reader.GetBoolean(9), reader.GetBoolean(10),
                    reader.GetBoolean(11), reader.GetBoolean(12),
                    reader.GetBoolean(13), reader.GetBoolean(14),
                    reader.GetBoolean(15)));
                setMonthlyRecurrence((string)reader.GetValue(17));
        }
        #endregion

        #region Get/Sets

        public int ScheduleID
        {
            get { return scheduleID; }
            set { scheduleID = value; }
        }
        public int ClientID
        {
            get { return clientID; }
            set { 
                if(value > 0)    
                    clientID = value; 
            }
        }
        public string Note
        {
            get { return note; }
            set { note = value; }
        }
        public SqlDateTime StartTime
        {
            get { return startTime; }
            set { startTime = value; }
        }
        public SqlDateTime EndTime
        {
            get { return endTime; }
            set {
                if (value < startTime)
                    endTime = startTime;
                else
                    endTime = value; 
            }
        }
        public SqlDateTime StartDate
        {
            get { return startDate; }
            set { 
                startDate = value;
                if (startDate > endDate)
                    endDate = value;
            }
        }
        public SqlDateTime EndDate
        {
            get { return endDate; }
            set {
                if (value < startDate)
                    endDate = startDate;
                else
                    endDate = value; 
            }
        }
        public string DaysOfMonth
        {
            get { return daysOfMonth; }
            set
            {
                Regex r = new Regex("[^,0-9]");
                daysOfMonth = r.Replace(value, "");
            }
        }

        #endregion

        #region Recurrence

        public void setRecurrence(bool isDaily, bool isWeekly, bool isMonthly)
        {
            daily = isDaily;
            weekly = isWeekly;
            monthly = isMonthly;
        }
        public void setWeeklyRecurrence(bool Monday, bool Tuesday, bool Wednesday, bool Thursday, bool Friday, bool Saturday, bool Sunday)
        {
            mon = Monday;
            tue = Tuesday;
            wed = Wednesday;
            thu = Thursday;
            fri = Friday;
            sat = Saturday;
            sun = Sunday;
        }

        //
        //allow input of weekly recurrence as a string of 0/1
        //e.g. Monday and Wednesday = "1010000"
        //
        public void setWeeklyRecurrence(string dayString)
        {
            mon = (dayString[0] == '1');
            tue = (dayString[1] == '1');
            wed = (dayString[2] == '1');
            thu = (dayString[3] == '1');
            fri = (dayString[4] == '1');
            sat = (dayString[5] == '1');
            sun = (dayString[6] == '1');
        }
        public void setMonthlyRecurrence(int[] days)
        {
            string s = "";
            for(int i=0; i<days.Length; i++)
            {
                if(days[i] < 31 && days[i] > 9)
                {
                    s = s + days[i].ToString();
                }
                else if(days[i] > 0 && days[i] < 10)
                {
                    s = s + "0"+days[i].ToString();
                }
            }
            daysOfMonth = s;
        }
        public void setMonthlyRecurrence(string days)
        {
            daysOfMonth = days;
        }

    #endregion

        #region SQL Statement Creation
        public SqlCommand InsertItem()
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO data ([ScheduleID],[ClientID],[StartTime],[EndTime],[StartDate],[EndDate],[Notes],"+
                "[Daily],[Weekly],[Monthly],[Mon],[Tue],[Wed],[Thu],[Fri],[Sat],[Sun],[DaysOfMonth]) " +
                "VALUES (@scheduleID,@clientID,@startTime,@endTime,@startDate,@endDate,@note,@daily,@weekly,@monthly," 
                + "@mon,@tue,@wed,@thu,@fri,@sat,@sun,@daysOfMonth)");
            SqlParameter p1 = new SqlParameter("@scheduleID",scheduleID);     cmd.Parameters.Add(p1);
            SqlParameter p2 = new SqlParameter("@clientID",clientID);         cmd.Parameters.Add(p2);
            SqlParameter p3 = new SqlParameter("@startTime",startTime);       cmd.Parameters.Add(p3);
            SqlParameter p4 = new SqlParameter("@endTime",endTime);           cmd.Parameters.Add(p4);
            SqlParameter p5 = new SqlParameter("@startDate",startDate);       cmd.Parameters.Add(p5);
            SqlParameter p6 = new SqlParameter("@endDate",endDate);           cmd.Parameters.Add(p6);
            SqlParameter p7 = new SqlParameter("@note",note);                 cmd.Parameters.Add(p7);
            SqlParameter p8 = new SqlParameter("@daily",daily);               cmd.Parameters.Add(p8);
            SqlParameter p9 = new SqlParameter("@weekly",weekly);             cmd.Parameters.Add(p9);
            SqlParameter p10 = new SqlParameter("@monthly",monthly);          cmd.Parameters.Add(p10);
            SqlParameter p11 = new SqlParameter("@mon",mon);                  cmd.Parameters.Add(p11);
            SqlParameter p12 = new SqlParameter("@tue",tue);                  cmd.Parameters.Add(p12);
            SqlParameter p13 = new SqlParameter("@wed",wed);                  cmd.Parameters.Add(p13);
            SqlParameter p14 = new SqlParameter("@thu",thu);                  cmd.Parameters.Add(p14);
            SqlParameter p15 = new SqlParameter("@fri",fri);                  cmd.Parameters.Add(p15);
            SqlParameter p16 = new SqlParameter("@sat",sat);                  cmd.Parameters.Add(p16);
            SqlParameter p17 = new SqlParameter("@sun",sun);                  cmd.Parameters.Add(p17);
            SqlParameter p18 = new SqlParameter("@daysOfMonth",daysOfMonth);  cmd.Parameters.Add(p18);
            return cmd;
        }
        #endregion

        #region Validation Helper Functions
        public bool isReady()
        {
            if (startTime.IsNull || endTime.IsNull || startDate.IsNull || clientID == null || scheduleID == null || note == "")
            {
                return false;
            }
            else
                return true;
        }

        public List<string> getDescriptionString()
        {
            List<string> descriptStrings = new List<string>();
            descriptStrings.Add("This event for client number " + clientID + " will display the following message: ");
            descriptStrings.Add("(" + note + ")");
            descriptStrings.Add("On " + String.Format("{0:d}", startDate) + " from " + String.Format("{0:t}", startTime) + " to " + String.Format("{0:t}", endTime));
     
            if (daily)
                descriptStrings.Add("This event will repeat daily until "+String.Format("{0:d}",endDate));
            string repeat = "";
            if (weekly)
                repeat += "This event will repeat weekly, every ";
            if (mon)
                repeat += "Monday, ";
            if (tue)
                repeat += "Tuesday, ";
            if (wed)
                repeat += "Wednesday, ";
            if (thu)
                repeat += "Thursday, ";
            if (fri)
                repeat += "Friday, ";
            if (sat)
                repeat += "Saturday, ";
            if (sun)
                repeat += "Sunday, ";
            repeat += "until "+String.Format("{0:d}",endDate);
            descriptStrings.Add(repeat);
            if (monthly)
                descriptStrings.Add("This event will repeat monthly on the following days: " + daysOfMonth);
            return descriptStrings;
        }

        private void setNull()
        {
            scheduleID = 0;
            clientID = 0;
            note = "";
            startTime = new SqlDateTime();
            endTime = new SqlDateTime();
            startDate = new SqlDateTime();
            endDate = new SqlDateTime();
            daily = weekly = monthly = false;
            mon = tue = wed = thu = fri = sat = sun = false;
            daysOfMonth = "";

        }
        #endregion
    }
}
